﻿using System;
using System.Collections;
using System.Collections.Generic;
using U3dUtils;
using UI00;
using UnityEngine;
using UnityEngine.UI;
namespace UI01{
    public partial class UI_Economics: UIBase{
        protected override string _GetUIPrefabPath(){
            return"UI01/MonoUI/UI_Economics/UI_Economics.prefab";
        }

        public static Transform parent;

        protected override Transform _GetUIParent(){
            if(parent != null){
                return parent;
            }
            return UIBase.uiLayer3_ui;
        }

        Button[] TabButtonTopArr = new Button[3];

        protected override void _Init(){ 
            for(int i = 0; i < 3; i++){
                TabButtonTopArr[i] = m_rootRt.GetControl<Button>("view/TabButtonTop0" + i);
            }
        }
        protected override void _Open(Dictionary<string, object> paraDic){}
        public void S_Refresh(Dictionary<int, Dictionary<int, C_ItemConfig>> dic, string[] nameArr){}

        /// <summary>
        /// 初始化选择框
        /// </summary>
        void S_InitSelectBox(Sprite[] imageArr, GameObject cardPrefab, Action<int> callback){
            cardPrefab.SetActive(false);
            RectTransform father = (RectTransform)cardPrefab.transform.parent;
            RectTransform mask = (RectTransform)father.parent;
            RectTransform card = cardPrefab.GetComponent<RectTransform>();
            int numberX = (int)(father.sizeDelta.x / card.sizeDelta.x);
            for(int i = 0; i < imageArr.Length; i++){
                GameObject gg = GameObject.Instantiate(cardPrefab);
                gg.SetActive(true);
                gg.transform.SetParent(cardPrefab.transform.parent);
                C_Item nn = new C_Item(gg);
                nn.root.localScale = new Vector3(1, 1, 1);
                nn.root.anchorMin = new Vector2(0, 1);
                nn.root.anchorMax = new Vector2(0, 1);
                nn.root.localPosition = card.localPosition + new Vector3((card.sizeDelta.x + 10) * (i % numberX), -(card.sizeDelta.y + 10) * (i / numberX), 0);
                gg.GetComponent<Image>().overrideSprite = imageArr[i];
                int ii = i;
                gg.GetComponent<Button>().
                    onClick.AddListener(delegate(){
                        callback(ii);
                    });
            }
            float high = (card.sizeDelta.y + 10) * ((imageArr.Length + numberX - 1) / numberX) + 20;
            if(mask.sizeDelta.y >= high){
                high = mask.sizeDelta.y;
            }
            father.sizeDelta = new Vector2(father.sizeDelta.x, high);
        }

        class C_Item{
            public int id;
            public RectTransform root;
            public Image icon;
            public Text number, name;

            public C_Item(GameObject gg){
                root = gg.GetComponent<RectTransform>();
            }
        }
        public class C_ItemConfig{
            public int id;
            public int currentNumber;
            public int totalNumber;
            public string name;
        }
    }
}
